import {action, observable, runInAction} from "mobx";
import {BaseResInterface, BaseResInterfaceWithData, sendGetRequest, sendPostRequest} from "../../../net";
import UserStoreInstance from "./index";
import {defaultArr, defaultObj} from "../../../util";


interface IncomeProfile {
    integral: number,
    withDrawnIntegral: number,
    todayIncome: number,
    notAllowWithDrawnIntegral: number
}

export interface IncomeIntegralByType {
    integral: string,
    type: number
}

export interface IncomeDetail {
    productIntegral: number,
    beginDate: Date,
    count: number,
    invalidCount: number,
    product: {
        imageUrl: string,
        infoImageUrl: string,
        name: string
    },
    user: {
        nickName: string
    }
}

export interface IncomeDetailRes {
    details: IncomeDetail[],
    type: number
}

export class IncomeStore {
    @observable incomeProfile: IncomeProfile = defaultObj;
    @observable incomeDetailList: IncomeDetailRes = defaultObj;

    @action.bound
    async getIncomeProfileByUserId() {
        const {data} = await sendGetRequest<BaseResInterfaceWithData<IncomeProfile>>("user/getUserIncomeProfile", {userId: UserStoreInstance.user.id});
        runInAction(() => {
            this.incomeProfile = data;
        })
    }


    async getUserIncomeModuleByDateRange(dateRange: Date[]) {
        const {data} = await sendPostRequest<BaseResInterfaceWithData<IncomeIntegralByType[]>>("user/getUserIncomeModuleByDateRange", {
            userId: UserStoreInstance.user.id,
            dateRange
        });
        return data;
    }

    @action.bound
    async getIncomeDetailByType(type: number, dateRange: Date[]) {
        const {data} = await sendPostRequest<BaseResInterfaceWithData<IncomeDetailRes>>("user/getIncomeDetailByType", {
            userId: UserStoreInstance.user.id,
            dateRange, type
        });
        console.log(data);
        runInAction(() => {
            this.incomeDetailList = data;
        });
        return data;
    }
}

export function getIncomeStoreInstance() {
    return new IncomeStore()
}

const IncomeStoreInstance = getIncomeStoreInstance();
export default IncomeStoreInstance